<template>
    <div class="login">
        <!-- 最外层的大盒子 -->
        <div class="box">
            <!-- 滑动的盒子 -->
            <div class="pre-box" ref="preRef">
                <h1>WELCOM</h1>
                <p>JION US!</p>
                <div class="img-box">
                    <img src="../../assets/pc1.jpeg">
                </div>
            </div>

            <!-- 注册盒子 -->
            <div class="register-form">
                <!-- 标题盒子 -->
                <div class="title-box">
                    <h1>注册</h1>
                </div>
                <!-- 输入框盒子 -->
                <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules">
                    <el-form-item label="账 号" prop="username">
                        <el-input v-model="registerForm.username" placeholder="请输入用户名" />
                    </el-form-item>
                    <el-form-item label="密 码" prop="password">
                        <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" show-password />
                    </el-form-item>
                    <el-form-item label="姓 名" prop="realName">
                        <el-input v-model="registerForm.realName" placeholder="请输入您的真实姓名" />
                    </el-form-item>
                    <el-form-item label="电 话" prop="phone">
                        <el-input v-model="registerForm.phone" placeholder="请输入电话号码" />
                    </el-form-item>
                    <el-form-item label="邮 箱" prop="mailbox">
                        <el-input v-model="registerForm.mailbox" placeholder="请输入您的邮箱" />
                    </el-form-item>

                </el-form>

                <div class="btn-box">
                    <button class="btn-register" @click="goRegister">注册</button>
                    <!-- 绑定点击事件 -->
                    <p @click="mySwitch">已有账号？去登录</p>
                </div>
            </div>

            <!-- 登录盒子 -->
            <div class="login-form">
                <div class="title-box">
                    <h1>登录</h1>
                </div>
                <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules">
                    <el-form-item label="账 号" prop="username">
                        <el-input v-model="loginForm.username" placeholder="请输入用户名" />
                    </el-form-item>
                    <el-form-item label="密 码" prop="password">
                        <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password />
                    </el-form-item>
                    <el-checkbox v-model="loginForm.checked" label="记住用户名和密码" size="large" />
                </el-form>
                <div class="btn-box">
                    <button class="btn-login" @click="gologin">登录</button>
                    <p @click="mySwitch">没有账号？去注册</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import mySwitch from './mySwitch'

import CryptoJS from "crypto-js";
import { login } from '../../api/sessions'

import {addMembers} from '../../api/member'

import { useRouter } from 'vue-router' 
import { toRefs } from 'vue';
const router = useRouter()

// 加解密密钥
const secret = "qwertyuioplkjhgfdss111";



// 注册信息
const registerForm = reactive({
    username: '',
    password: '',
    realName: '',
    phone: '',
    mailbox: ''
})
// 注册表单校验
const registerRules = reactive({
    username: [
        {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
        }, 
        {
            min: 3,
            max: 10,
            message: '用户名长度在3-10个字符之间',
            trigger: 'blur',
        }
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
        },
        {
            min: 4,
            max: 9,
            required: true,
            message: '密码长度在4-9个字符之间',
            trigger: 'blur',
        }
    ],
    realName: [
        {
            required: true,
            message: '请输入真实姓名',
            trigger: 'blur',
        },
        {
            required: true,
            pattern: /^[\u4e00-\u9fa5]{2,7}$/,
            message: '姓名必须是2-7个中文字符',
            trigger: 'blur',
        }
    ],
    phone: [
        {
            required: true,
            message: '请输入电话号码',
            trigger: 'blur',
        },
        {
            pattern: /^[\d]{6,11}$/,
            message: '电话号码为6-11位数字',
            trigger: 'blur',
        }
    ]
})

const registerFormRef = ref('')

// 登录信息
const loginForm = reactive({
    username: '',
    password: '',
    checked: false,
})
// 登录表单校验
const loginRules = reactive({
    username: [
        {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
        },
    ],
    password: [
        {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
        }
    ]
})
const loginFormRef =ref('')

// 登录功能
async function gologin() {
    try {
        const valid = await loginFormRef.value.validate();
        if (valid) {
            const data = await login(loginForm)
            const { token } = data
            // 登录成功
            if (data) {
                console.log(data)
                // 将登录用户的 token 保存到本地
                sessionStorage.setItem("token", token)
                if (loginForm.checked) {
                    const info = loginForm.username + "::::::" + loginForm.password;
                    const hash = CryptoJS.AES.encrypt(info, secret).toString();
                    localStorage.setItem("userinfo", hash);
                } else {
                    localStorage.removeItem("userinfo");
                }
                // 跳转页面
                router.push('/')
            }
        }
    } catch (error) {
        console.log('登录异常:', error)
    }
}





// 注册功能
async function goRegister() {
    try {
        const valid = await registerFormRef.value.validate();
        if (valid) {
            const result = addMembers(registerForm)
            const {code} = result
        }
    } catch (error) {
        console.log('登录异常:', error)
    }
}

// 账号密码回显
onMounted(() => {
    const hash = localStorage.getItem('userinfo')
    if(hash) {
        const info = CryptoJS.AES.decrypt(hash, secret).toString(CryptoJS.enc.Utf8).split('::::::')
        // 使用torefs把reactive对象解构成ref
        const {username,password,checked} = toRefs(loginForm)
        username.value = info[0]
        password.value = info[1]
        checked.value = true
    }
})
   
</script>

<style scoped>
input {
    outline: none;
}

html,
body {
    height: 100%;
}

.login {
    overflow: hidden;
    height: 100vh;
    display: flex;
    background: linear-gradient(to right, rgb(230, 239, 244), rgb(86, 164, 237));
}

span {
    position: absolute;
    z-index: 0;
    bottom: 0;
    border-radius: 50%;
    background: radial-gradient(circle at 72% 28%, #fff 3px, #ff7edf 10px);
}

@keyframes myMove {
    0% {
        transform: translateY(0%);
        opacity: 1;
    }

    50% {
        transform: translate(10%, -1000%);
    }

    75% {
        transform: translate(-20%, -1200%);
    }

    99% {
        opacity: .9;
    }

    100% {
        transform: translateY(-1800%) scale(1.5);
    }
}

.box {
    width: 1200px;
    height: 600px;
    display: flex;
    position: relative;
    z-index: 2;
    margin: auto;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, .6);
    box-shadow: 2px 1px 19px rgba(0, 0, 0, .1);
}

.pre-box {
    width: calc(1200px / 2);
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 999;
    border-radius: 6px;
    background: #96C5F0;
    box-shadow: 2px 1px 19px rgba(0, 0, 0, .1);
    transition: 0.5s ease-in-out;
}

.pre-box h1 {
    margin-top: 150px;
    text-align: center;
    letter-spacing: 5px;
    color: #ffffff;
    user-select: none;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

/* 滑动盒子的文字 */
.pre-box p {
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin: 20px 0;
    user-select: none;
    font-weight: bold;
    color: #ffffff;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

.img-box {
    width: 200px;
    height: 200px;
    margin: 20px auto;
    border-radius: 50%;
    user-select: none;
    overflow: hidden;
    box-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

.img-box img {
    width: 100%;
    height: 100%;
    transition: 0.5s;
}

/* 登录和注册盒子 */
.login-form,
.register-form {
    flex: 1;
    height: 100%;
}

.el-checkbox {
    margin-left: -140px;
    color: #ffffff;
}

.title-box {
    height: 100px;
    line-height: 100px;
}

.title-box h1 {
    text-align: center;
    color: #ffffff;
    /* 禁止选中 */
    user-select: none;
    letter-spacing: 5px;
    text-shadow: 4px 4px 3px rgba(0, 0, 0, .1);
}

.el-form {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* 输入框 */
.el-form-item {
    width: 65%;
    height: 40px;
}

.el-input {
    height: 100%;
}

input:focus {
    /* 光标颜色 */
    color: #0b0b0b;
}

/* 聚焦时隐藏文字 */
input:focus::placeholder {
    opacity: 0;
}

.btn-box {
    /* margin: 0 auto; */
    display: flex;
    flex-direction: column;
    align-items: center;
}

button {
    width: 230px;
    height: 50px;
    margin: 0 7px;
    line-height: 50px;
    border: 0;
    border-radius: 4px;
    background: #69b3f0;
    border-radius: 30px;
    color: #ffffff;
}

/* 按钮悬停时 */
button:hover {
    cursor: pointer;
    opacity: .8;
}

/* 按钮文字 */
.btn-box p {
    height: 30px;
    line-height: 30px;
    /* 禁止选中 */
    user-select: none;
    font-size: 14px;
    color: #ffffff;
}

.btn-box p:hover {
    cursor: pointer;
}
</style>
